import { message } from 'antd';
import moment from 'moment'
import {
  getEmployeeById,
  updateMainInfo,
  updateHireDate
} from '../service';

const Model = {
  namespace: 'infoAndEdit',
  state: { info: {} },
  effects: {
    *updateMainInfo({ payload }, { call }) {
      let date = moment(payload.hiredate).format('YYYY-MM-DD')
      if(payload.updateHireDate !== date){
        updateHireDate(payload.oid,payload.updateHireDate).then()
      }
      const response = yield call(updateMainInfo, payload);
      if (response.flag) {
        message.success('修改成功');
      } else {
        message.error('修改失败');
      }
    },
    *getEmployeeById({ payload }, { call, put }) {
      const response = yield call(getEmployeeById, payload);
      yield put({
        type: 'show',
        payload: response.data,
      });
    },
  },
  reducers: {
    show(state, { payload }) {
      return { ...state, info: payload };
    },
  },
};
export default Model;
